﻿using Kimd.Common;
using Kimd.Communication;
using Kimd.Logic;
using Kimd.Machine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WorkStation.Common;

namespace WorkStation.Logic
{
    public class FeedPLCStatusLogic : FlowBase
    {
        //private PLCOperator _plc;
        //plc通信读地址
        public const int PLC_COM_READ_ADDRESS = 800;
        public const int PLC_COM_READ_WARN_ADDRESS_START = 400;
        //plc通信写地址
        public const int PLC_COM_WRITE_ADDRESS = 900;
        private bool[] inStatus = null;
        public FeedPLCStatusLogic(string flowName, FlowManager flowmanager) 
            : base(flowName, flowmanager)
        {
            //if(MachineResource.Instance.MachinePLC.ContainsKey("上料机PLC"))
            //    _plc = MachineResource.Instance.MachinePLC["上料机PLC"];
        }

        public override void Calibrate()
        {
            base.Calibrate();
        }

        public override void Home()
        {
            base.Home();
        }

        public override void Init(out string errInfo)
        {
            base.Init(out errInfo);
        }

        short[] vals = null;
        public override void Method()
        {
            if (GlobalVariable.FeedPLC == null)
            {
                GlobalVariable.FeedPLC = new ModbusTcp("192.168.10.101", 502, 0);
                GlobalVariable.FeedPLC.Connect();
            }
            if (isSimulate)
                return;
            if (!GlobalVariable.FeedPLC.IsConnected)
            {
            reconnect:
                GlobalVariable.FeedPLC.Connect();
                if (!GlobalVariable.FeedPLC.IsOpenFlag)
                {
                    MachineMessageScheduler.Instance.Add(new MachineMessage(MessageType.Warn, $"[上料站PLC]连接失败!", this.Description, 7, "轨道"));
                    //出错，停下所有线程
                    _flowManager.Pause();
                    //若为报错，需重新检测启动信号
                    WaitForStart();
                    if (!CheckFlowCondition()) return;
                    goto reconnect;
                }
            }
            GlobalVariable.FeedPLC.ReadBool("800", 2, out inStatus);
            if (inStatus != null)
            {
                //M800 = 1,扫码成功 feedingcheck
                if(inStatus[0])
                {
                    Thread.Sleep(40);
                    GlobalVariable.FeedPLC.ReadShort("400", 25, out vals);
                    if (vals != null)
                    {
                        string barcode = CommonMethod.ConvertShortArrayToString(vals);
                        if(SystemConfig.Instance.MesParam.EnableMES && !CommonMethod.DoFeedingCheck(barcode, out string errInfo))
                        {
                            if (SystemConfig.Instance.MesParam.EnableMESFeedingCheck)
                            {
                                GlobalVariable.FeedPLC.WriteBool("900", new bool[1] { true });
                                Thread.Sleep(1000);
                                GlobalVariable.FeedPLC.WriteBool("900", new bool[1] { false });
                            }
                            else
                            {
                                GlobalVariable.FeedPLC.WriteBool("901", new bool[1] { true });
                                Thread.Sleep(1000);
                                GlobalVariable.FeedPLC.WriteBool("901", new bool[1] { false });
                                ShowWarnAndPause($"MES FeedingCheck失败!{errInfo}", "系统");
                                if (!CheckFlowCondition()) return;
                            }
                        }
                        else
                        {
                            GlobalVariable.FeedPLC.WriteBool("900", new bool[1] { true });
                            Thread.Sleep(1000);
                            GlobalVariable.FeedPLC.WriteBool("900", new bool[1] { false });
                        }
                    }
                }
            }
            System.Threading.Thread.Sleep(100);
        }

        public override void OnStop()
        {
            base.OnStop();
        }

        public override bool PreStartCheck()
        {
            return base.PreStartCheck();
        }

        public override void SpotCheck()
        {
            base.SpotCheck();
        }
    }
}
